package exception;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Exercise {
    public static void main(String[] args) throws IOException {

        Animal dd = new Animal();

        try {
            dd.setAge(99);
            System.out.println("狗狗今年"+dd.getAge()+"岁了");

        }catch (RuntimeException e){
            System.out.println("年龄不合法");
        }

        Animal bd = new Animal();
        try {
            bd.setAge(5);
            System.out.println("狗狗今年"+bd.getAge()+"岁了");

        }catch (RuntimeException e){
            System.out.println("年龄不合法");
        }




        /*
        System.out.println("程序开始了");

        try(FileOutputStream fos =new FileOutputStream("fos.dat");){
            fos.write(1);
        }catch (IOException e){
            e.printStackTrace();
        }
        System.out.println("程序已结束");

        System.out.println("程序开始了");

        try(FileOutputStream fos =new FileOutputStream("fos.dat");){
            fos.write(1);
        }catch (IOException e){
            e.printStackTrace();
        }
        System.out.println("程序已结束");





        System.out.println("程序开始·");

        try {
            String line="";
            System.out.println(line.length());
            return;
        }catch (Exception e){
            System.out.println("出错并解决了");
        }finally {
            System.out.println("finally中的代码执行了");
        }
        System.out.println("程序结束了");



        System.out.println("程序开始·");

        try {
            String line="";
            System.out.println(line.length());
            return;
        }catch (Exception e){
            System.out.println("出错并解决了");
        }finally {
            System.out.println("finally中的代码执行了");
        }
        System.out.println("程序结束了");




        try {
            String line =null;
            System.out.println(line.length());
            String line1="";
            System.out.println(line1.charAt(0));
            String line2="123";
            System.out.println(Integer.parseInt(line2));
        }catch (NullPointerException e){
            System.out.println("null");
        }catch (StringIndexOutOfBoundsException e){
            System.out.println("出现了字符串下标越界异常并解决了");
        }catch (Exception e){
            System.out.println("fault");
        }
        System.out.println("程序结束了");



        try {
            String line =null;
            System.out.println(line.length());
            String line1="";
            System.out.println(line1.charAt(0));
            String line2="123";
            System.out.println(Integer.parseInt(line2));
        }catch (NullPointerException e){
            System.out.println("null");
        }catch (StringIndexOutOfBoundsException e){
            System.out.println("出现了字符串下标越界异常并解决了");
        }catch (Exception e){
            System.out.println("fault");
        }
        System.out.println("程序结束了");





        FileInputStream fis =new FileInputStream("pw.txt");
        InputStreamReader isr =new InputStreamReader(fis,StandardCharsets.UTF_8);
        BufferedReader br=new BufferedReader(isr);

        String line;
        while ((line= br.readLine())!=null){
            System.out.println(line);
        }
        br.close();



        FileInputStream fis = new FileInputStream("pw.txt");
        InputStreamReader isr =new InputStreamReader(fis,StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line= br.readLine())!=null){
            System.out.println(line);
        }
        br.close();




        FileOutputStream fos = new FileOutputStream("pw.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos,StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入内容，单独输入esc时退出");
        while (true){
            String line =scan.nextLine();
            if ("esc".equals(line)){
                break;
            }
            pw.println(line);
        }
        pw.close();



        FileOutputStream fos = new FileOutputStream("pw.txtx");
        OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);

        Scanner scan = new Scanner(System.in);
        System.out.println("请开始输入内容，单独输入exit时退出");
        while (true) {
            String line = scan.nextLine();
            if ("exit".equals(line)){
                break;
            }
            pw.println(line);
        }
        pw.close();
         */

    }
}
